/*  Neu - Feed Aggregator.
 *  Copyright (C) 2009  Joshua Montgomery
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details. 
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.neu.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.log4j.Logger;

/**Used for interaction with a MySql Database.
 * 
 * @author Joshua Montgomery
 * 
 */
public class MySqlConnection {
	
	private Logger logger = Logger.getLogger(MySqlConnection.class.getName());
	
	/**The connection to the MySql database*/
	private Connection connection = null;
	
	/**Default Constructor
	 * 
	 * @param URL The database to connect to. Form: //ip_address/database
	 * @param userName User name for the database.
	 * @param password Password for the database.
	 */
	public MySqlConnection(String URL, String userName,String password)
	{
		 try
         {            
             Class.forName ("com.mysql.jdbc.Driver").newInstance ();
             connection = DriverManager.getConnection ("jdbc:mysql:" + URL, userName, password);
             logger.debug("Database connection established!");
         }
         catch (Exception e)
         {
            logger.error("Cannot connect to database server",e);
         }
	}
	
	/**Retrieves the MySqlConnection.
	 * 
	 * @return The connection.
	 */
	public Connection getConnection()
	{
		return connection;
	}
	
	/**Close the database connection.*/
	public void close()
	{
		try {
			connection.close();
		} catch (SQLException e) {
			logger.error("An error occured closing the Database connection.",e);
		}	
		logger.debug("Database connection closed!");
	}

}
